package per.chao.mqtt.domain.facade.impl;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import per.chao.mqtt.domain.facade.IMessageCache;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author W.Chao
 * @date 2021/6/20 15:06
 **/
@Component("redisTopicCache")
@Slf4j
public class RedisTopicCache implements IMessageCache {
	private Map<String, Object> cache = Maps.newConcurrentMap();

	@Override
	public void put(String key, Object o) {
		cache.put(key, o);
	}

	@Override
	public Object get(String key) {
		return cache.get(key);
	}

	@Override
	public void remove(String key) {
		cache.remove(key);
	}

	@Override
	public boolean containsKey(String key) {
		return cache.containsKey(key);
	}

	@Override
	public Set<String> keys() {
		return cache.keySet();
	}

	@Override
	public Set<String> keys(final String prefixFilter) {
		return cache.keySet().stream()
				.filter(key -> key.substring(0, Math.min(key.length(), prefixFilter.length())).equals(prefixFilter))
				.collect(Collectors.toSet());
	}
}
